package org.geometerplus.android.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import java.util.Set;

import logic.util.Util;


public class QyreaderPerferencesUtil
{
	private SharedPreferences              preferences;

	private Editor                         editor;
	private String                         account;
	private static QyreaderPerferencesUtil instance;
	private Context                        context;
	private static String                  ADVERTISEMENT_DOWNLOADPATH       = "advertisement_downloadpath";
	private static String                  ADVERTISEMENT_IMAGEURL           = "advertisement_url";
	private static String                  ADVERTISEMENT_LINKEURL           = "advertisement_linkurl";
	private static String                  ADVERTISEMENT_TITLE              = "advertisement_title";
	private static String                  ADVERTISEMENT_IMAGELENGTH        = "advertisement_imagelength";
	private static String                  ADVERTISEMENT_SHOWTIME           = "advertisement_showtime";
	private static String                  EXIST_MAIN_ACTIVITY              = "exist_main_activity";
	private static String                  EXIST_BOOKSHELF_PAGE             = "exist_bookshelf_page";
	private static String                  LAST_PUSH_MESSAGE_ID             = "last_push_message_id";
	private static String                  BOOKSHELF_MESSAGE_IDS            = "bookshelf_message_ids";
	private static String                  PUSH_SETTING                     = "push_setting";
	private static String                  PUSH_MESSAGE                     = "push_message";
	private static String                  SHOW_NOTICE                      = "show_notice";
	private static String                  SHOW_ZHENDONG                    = "show_zhendong";
	public static String                   CURRENTCHAPTER                   = "currentchapter_";
	public static String                   CURRENTCHAPTER_LOCAL             = "currentchapter_loal";
	public static String                   DOWNLOADCHAPTER                  = "downloadchapter_";
	private static String                  UPDATE_VERSION_DOWNLOAD_COMPLETE = "update_version_download_complete";
	private static String                  FIRST_READING                    = "first_reading_in";
	private static String                  FIRST_ECARD_LOGIN                = "first_ecard_login";
	private static String                  FIRST_BOUTIQUE_LOGIN             = "first_boutique_login";
	private static String                  FIRST_LBS                        = "first_lbs";
	private static String                  LASTCHECKTIME                    = "last_Check_Time";
	private static String                  LASTCHECKTIME_ECOMMERCE          = "last_Check_Time_ECommerce";
	private static String                  TRAIN_EXAM_INDEX_CID             = "train_exam_index_cid";
	private static String                  QRY_UN_READ_ACTIVItY             = "qry_unread_activity";
	private static String                  IS_SHOW_LOCATION_FAIL_DIALOG     = "is_show_location_fail_dailog";
	private static String                  CONTACT_LAST_TIME_STAMP          = "contact_last_time_stamp";
	private static String                  HEALTH_DAY_DATA                  = "health_day_data";
	private static String                  HEALTH_DAY_TOTAL_DATA            ="health_day_total_data";
	private static String                  LAST_MSG_TIME_STAMP          = "last_msg_time_stamp";

	private QyreaderPerferencesUtil(Context context, String account) {
		this.context = context;
		this.account = account;
		String fileName = "QYReader" + account;
		preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		editor = preferences.edit();
	}

	public static QyreaderPerferencesUtil getInstance(Context context, String account) {
		if (instance == null || instance.account != account)
		{
			instance = new QyreaderPerferencesUtil(context, account);
		}
		return instance;
	}

	public SharedPreferences getSharedPreferences() {
		return preferences;
	}

	public Editor getEditor() {
		return editor;
	}

	/**
	 * 清空所有数据
	 */
	public void clearAllData() {
		editor.clear();
		editor.commit();
	}

	public void setLastPushMessageId(long messageId) {
		editor.putLong(LAST_PUSH_MESSAGE_ID, messageId);
		editor.commit();
	}

	public long getLastPushMessageId() {
		return preferences.getLong(LAST_PUSH_MESSAGE_ID, 0L);
	}

	//通讯录调用接口时间戳

	public long getContactLastTimeStamp() {
		return preferences.getLong(CONTACT_LAST_TIME_STAMP, 0L);
	}

	public void setContactLastTimeStamp(long value) {
		editor.putLong(CONTACT_LAST_TIME_STAMP, value);
		editor.commit();
	}
	//最后一条消息的时间戳
	public long getLastMsgTimeStamp() {
		return preferences.getLong(LAST_MSG_TIME_STAMP, 0L);
	}

	public void setLastMsgTimeStamp(long value) {
		editor.putLong(LAST_MSG_TIME_STAMP, value);
		editor.commit();
	}

	//广告页标题
	public String getAdvertisementTitle() {
		return preferences.getString(ADVERTISEMENT_TITLE, "");
	}

	public void setAdvertisementTitle(String value) {
		editor.putString(ADVERTISEMENT_TITLE, value);
		editor.commit();
	}

	//广告页保存地址
	public String getAdvertisementDownloadPath() {
		return preferences.getString(ADVERTISEMENT_DOWNLOADPATH, "");
	}

	public void setAdvertisementDownloadPath(String value) {
		editor.putString(ADVERTISEMENT_DOWNLOADPATH, value);
		editor.commit();
	}

	//广告页下载地址
	public String getAdvertisementImageUrl() {
		return preferences.getString(ADVERTISEMENT_IMAGEURL, "");
	}

	public void setAdvertisementImageUrl(String value) {
		editor.putString(ADVERTISEMENT_IMAGEURL, value);
		editor.commit();
	}
	private static String                  AREA_TIME_STAMP          = "area_time_stamp";
	//省市区公司增量接口时间戳
	public long getAreaLastTimeStamp() {
		return preferences.getLong(AREA_TIME_STAMP, 0L);
	}

	public void setAreaLastTimeStamp(long value) {
		editor.putLong(AREA_TIME_STAMP, value);
		editor.commit();
	}

	//广告页外链接地址

	public String getAdvertisementLinkUrl() {
		return preferences.getString(ADVERTISEMENT_LINKEURL, "");
	}

	public void setAdvertisementLinkUrl(String value) {
		editor.putString(ADVERTISEMENT_LINKEURL, value);
		editor.commit();
	}

	//广告页长度
	public int getAdvertisementImageLength() {
		return preferences.getInt(ADVERTISEMENT_IMAGELENGTH, 0);
	}

	public void setAdvertisementImageKLength(int value) {
		editor.putInt(ADVERTISEMENT_IMAGELENGTH, value);
		editor.commit();
	}

	//广告页时间
	public int getAdvertisementShowTime() {
		return preferences.getInt(ADVERTISEMENT_SHOWTIME, 0);
	}

	public void setAdvertisementShowTime(int value) {
		editor.putInt(ADVERTISEMENT_SHOWTIME, value);
		editor.commit();
	}

	//保存下载的当前长度
	public void setDownLoadSize(String bookId, String size) {
		editor.putString(bookId, size);
		editor.commit();
	}

	public String getDownLoadSize(String bookId) {
		return preferences.getString(bookId, "");
	}

	//关于页面的推送设置
	public boolean getPushSet() {
		return preferences.getBoolean(PUSH_SETTING, true);
	}

	public void setPushSet(boolean value) {
		editor.putBoolean(PUSH_SETTING, value);
		editor.commit();
	}

	//推送设置界面 消息推送
	public boolean getPushMessage() {
		return preferences.getBoolean(PUSH_MESSAGE, true);
	}

	public void setPushMessage(boolean value) {
		editor.putBoolean(PUSH_MESSAGE, value);
		editor.commit();
	}

	/** 推送设置界面 通知铃声 */
	public boolean getShowNotice() {
		return preferences.getBoolean(SHOW_NOTICE, true);
	}

	/** 推送设置界面 通知铃声 */
	public void setShowNotice(boolean value) {
		editor.putBoolean(SHOW_NOTICE, value);
		editor.commit();
	}

	/** 推送设置界面 通知震动 */
	public boolean getShowZhendong() {
		return preferences.getBoolean(SHOW_ZHENDONG, true);
	}

	/** 推送设置界面 通知震动 */
	public void setShowZhendong(boolean value) {
		editor.putBoolean(SHOW_ZHENDONG, value);
		editor.commit();
	}

	/**
	 * 书本当前章节目录
	 *
	 * @param bookId
	 * @param index
	 */
	public void setChapterIndex(String bookId, int index) {
		editor.putInt(CURRENTCHAPTER + bookId, index);
		editor.commit();
	}

	public int getChapterIndex(String bookId) {
		return preferences.getInt(CURRENTCHAPTER + bookId, 0);
	}

	/**
	 * 本地书本当前章节目录
	 *
	 * @param bookId
	 * @param
	 */
	public void setLocalIndex(String bookId, int index) {
		editor.putInt(CURRENTCHAPTER_LOCAL + bookId, index);
		editor.commit();
	}

	public int getLocalIndex(String bookId) {
		return preferences.getInt(CURRENTCHAPTER_LOCAL + bookId, 0);
	}

	/**
	 * 下载章节目录
	 *
	 * @param bookId
	 * @param chapterIndex
	 */
	public void setDownloadChapter(String bookId, int chapterIndex) {
		editor.putInt(DOWNLOADCHAPTER + bookId, chapterIndex);
		editor.commit();
	}

	public int getDownloadChapterindex(String bookId) {
		return preferences.getInt(DOWNLOADCHAPTER + bookId, 0);
	}

	//获取版本更新是否完成
	public boolean getUpdateVersionHaveDownLoadComplete() {
		return preferences.getBoolean(UPDATE_VERSION_DOWNLOAD_COMPLETE, false);
	}

	//设置版本更新状态
	public void setUpdateVersionDownLoadComplete(boolean complete) {
		Log.i("yhx", "更新软件更新状态     " + complete);
		editor.putBoolean(UPDATE_VERSION_DOWNLOAD_COMPLETE, complete);
		editor.commit();
	}

	//设置查询服务到期时的请求时间
	public void setQryUseServiceFinishedActionTime(long qryTime) {
		editor.putLong("qrytime", qryTime);
		editor.commit();
	}

	public long getQryUseServiceFinishedActionTime() {
		return preferences.getLong("qrytime", 0);
	}

	public void setExistMainActivity(boolean value) {
		editor.putBoolean(EXIST_MAIN_ACTIVITY, value);
		editor.commit();
	}

	public boolean getExistMainActivity() {
		return preferences.getBoolean(EXIST_MAIN_ACTIVITY, true);
	}

	public void setExistBookShelfPage(boolean value) {
		editor.putBoolean(EXIST_BOOKSHELF_PAGE, value);
		editor.commit();
	}

	public boolean getExistBookShelfPage() {
		return preferences.getBoolean(EXIST_BOOKSHELF_PAGE, false);
	}

	/********** 是否是第一次启动阅读界面 ************/
	public boolean isFirstReading() {
		return preferences.getBoolean(FIRST_READING, true);
	}

	public void setFirstReading(boolean isfirstReading) {
		editor.putBoolean(FIRST_READING, isfirstReading);
		editor.commit();
	}

	public boolean isFirstEcardLogin() {
		return preferences.getBoolean(FIRST_ECARD_LOGIN, true);
	}

	public void setFirstEcardLogin(boolean value) {
		editor.putBoolean(FIRST_ECARD_LOGIN, value);
		editor.commit();
	}

	public boolean getShowLoginHelp() {
		return preferences.getBoolean("show_login_help", true);
	}

	public void setShowLoginHelp(boolean value) {
		editor.putBoolean("show_login_help", value);
		editor.commit();
	}

	public boolean getShowEnterpriseHelp() {
		return preferences.getBoolean("show_enterprise_help", true);
	}

	public void setShowEnterpriseHelp(boolean value) {
		editor.putBoolean("show_enterprise_help", value);
		editor.commit();
	}

	public boolean getShowGpsOpen() {
		return preferences.getBoolean("gps_open", true);
	}

	public void setShowGpsOpen(boolean value) {
		editor.putBoolean("gps_open", value);
		editor.commit();
	}

	public boolean isFirstBoutiqueLogin() {
		return preferences.getBoolean(FIRST_BOUTIQUE_LOGIN, true);
	}

	public void setFirstBoutiqueLogin(boolean value) {
		editor.putBoolean(FIRST_BOUTIQUE_LOGIN, value);
		editor.commit();
	}

	public boolean isFirstLBS() {
		return preferences.getBoolean(FIRST_LBS, true);
	}

	public void setFirstLBS(boolean value) {
		editor.putBoolean(FIRST_LBS, value);
		editor.commit();
	}

	//检查是否有我的新评论的最后时间
	public void setLastCheckTime(long lastTime) {
		editor.putLong(LASTCHECKTIME, lastTime);
		editor.commit();
	}

	public long getLastCheckTime() {
		return preferences.getLong(LASTCHECKTIME, 0L);
	}

	/** 检查书架消息是否显示过 */
	public boolean checkMsgId(long msgId) {
		final String str = preferences.getString(BOOKSHELF_MESSAGE_IDS, "");
		if (Util.isEmpty(str)) return false;
		final String[] arr = str.split(",");
		for (int i = 0; i < arr.length; i++)
		{
			if (Long.valueOf(arr[i]) == msgId) return true;
		}
		return false;
	}

	/** 显示过的书架消息，进行存储 */
	public void addMsgId(long msgId) {
		String str = preferences.getString(BOOKSHELF_MESSAGE_IDS, "");
		editor.putString(BOOKSHELF_MESSAGE_IDS, Util.isEmpty(str) ? String.valueOf(msgId) : str + "," + msgId);
		editor.commit();
		str = null;
	}

	//检查是否有我的商品动态的最后时间
	public void setLastCheckTimeECommerce(long lastTime) {
		editor.putLong(LASTCHECKTIME_ECOMMERCE, lastTime);
		editor.commit();
	}

	//检查是否有我的商品动态的最后时间
	public long getLastQryTime() {
		return preferences.getLong(QRY_UN_READ_ACTIVItY, 0);
	}

	//检查是否有我的商品动态的最后时间
	public void setLastQryTime(long lastTime) {
		editor.putLong(QRY_UN_READ_ACTIVItY, lastTime);
		editor.commit();
	}

	//检查是否有我的商品动态的最后时间
	public long getLastCheckTimeECommerce() {
		return preferences.getLong(LASTCHECKTIME_ECOMMERCE, 0);
	}

	//培训首页cid
	public void setTrainIndexCid(String value) {
		editor.putString(TRAIN_EXAM_INDEX_CID, value);
		editor.commit();
	}

	public String getTrainIndexCid() {
		return preferences.getString(TRAIN_EXAM_INDEX_CID, "");
	}

	public boolean getIsShowLocationFailDialog() {
		return preferences.getBoolean(IS_SHOW_LOCATION_FAIL_DIALOG, true);
	}

	public void setIsShowLocationFailDialog(boolean value) {
		editor.putBoolean(IS_SHOW_LOCATION_FAIL_DIALOG, value);
		editor.commit();
	}

	public Set<String> getHealthDayData() {
		return preferences.getStringSet(HEALTH_DAY_DATA,null);
	}

	public void  setHealthDayData(Set<String> datas) {
		editor.putStringSet(HEALTH_DAY_DATA, datas);
		editor.commit();
	}
	public Set<String> getHealthDayTotalData() {
		return preferences.getStringSet(HEALTH_DAY_TOTAL_DATA,null);
	}

	public void  setHealthDayTotalData(Set<String> datas) {
		editor.putStringSet(HEALTH_DAY_TOTAL_DATA, datas);
		editor.commit();
	}



}
